package com.liupengyuan.framework.web.query;

import java.util.Collections;
import java.util.List;

@SuppressWarnings({ "rawtypes" })
public class GenericDefaultPage implements IGenericPage {
	private int pageNo;
	private int pageSize;
	private int totalCount;
	private List elements;
	public static final GenericDefaultPage EMPTY_PAGE = new GenericDefaultPage(
			0, 0, Collections.emptyList(), 0);
	
	/**
	 * 最后一页的页面号
	 */
	public int getLastPageNo() {
		if(getPageSize()<=0)return 0;
		return getTotalCount()*1.0 % getPageSize()>0?
				getTotalCount() / getPageSize()+1:
					getTotalCount() / getPageSize();
	}
	/**
	 * 后一页的页号
	 */
	public int getNextPageNo() {
		return getPageNo()+1;
	}
	/**
	 * 前一页的页号
	 */
	public int getPreviousPageNo() {
		return getPageNo() - 1;
	}
	/**
	 * 当前页的元素
	 */
	public List getThisPageElements() {
		return hasNextPage() ? elements.subList(0, pageSize) : elements;
	}
	/**
	 * 当前面第一个元素的下序号
	 */
	public int getThisPageFirstElementIndex() {
		return (getPageNo()-1) * getPageSize();
	}
	/**
	 * 当前面最后的元素的下序号
	 */
	public int getThisPageLastElementIndex() {
		int fullNumber = getPageNo() * getPageSize() - 1;
		return !isLastPage() ? fullNumber : getTotalCount()-1;
	}
	/**
	 * 是否下页
	 */
	public boolean hasNextPage() {
		return elements !=null ? elements.size()>getPageSize():false;
	}
	/**
	 * 是否有前页
	 */
	public boolean hasPreviousPage() {
		return getPageNo()>0;
	}
	/**
	 * 是否为首页
	 */
	public boolean isFirstPage() {
		return getPageNo()<=1;
	}
	/**
	 * 是否为尾页
	 */
	public boolean isLastPage() {
		return getPageNo() >= getLastPageNo();
	}
	
	
	
	
	
	public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		if(pageNo<=0) pageNo=1;
		this.pageNo = pageNo;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public List getElements() {
		return elements;
	}
	public void setElements(List elements) {
		this.elements = elements;
	}
	
	public GenericDefaultPage(int pageNo, int pageSize, List elements, int totalCount ) {
		setPageNo(pageNo);
		setPageSize(pageSize);
		setTotalCount(totalCount);
		setElements(elements);
	}
	
	public static int getStartOfPage(int pageNo , int pageSize){
		int startIndex = (pageNo - 1) * pageSize + 1;
		if (startIndex < 1)
			startIndex = 1;
		return startIndex;
	}
	public static GenericDefaultPage emptyPage(){
		return EMPTY_PAGE;
	}
}
